package com.soft.bams.entity;

import com.soft.bams.exception.BalanceNotEnoughException;
import com.soft.bams.exception.LoanException;

import java.io.Serializable;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/21
 */
public class LoanSavingAccount extends SavingAccount implements Loanable{
    private long loanAmount;  // 贷款金额

    public LoanSavingAccount() {
        super();
    }

    public LoanSavingAccount(Long id, String password, String name, String personId,
                             String email, double balance) {
        super(id, password, name, personId, email, balance);
    }

    @Override
    public Account requestLoan(double money) throws LoanException {
        if (money > 0) {
            this.loanAmount += (long) money;
            this.deposit(money);  // 贷款金额存入账户
        }else {
            throw new LoanException("贷款额须大于零");
        }
        return this;
    }

    @Override
    public Account payLoan(double money) throws BalanceNotEnoughException {
        if (money > 0 && this.getBalance() >= money && this.loanAmount >= money) {
            this.withdraw(money);  // 从余额中扣除
            this.loanAmount -= (long) money;
        }
        return this;
    }

    public long getLoanAmount() {
        return loanAmount;
    }

    public void setLoanAmount(long loanAmount) {
        this.loanAmount = loanAmount;
    }

    @Override
    public String toString() {
        return "LoanSavingAccount{" +
                "id=" + getId() +
                ", password='" + getPassword() + '\'' +
                ", name='" + getName() + '\'' +
                ", personId='" + getPersonId() + '\'' +
                ", email='" + getEmail() + '\'' +
                ", balance=" + getBalance() +
                ", loanAmount=" + loanAmount +
                '}';
    }
}